﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
//Wg. Performance Tests

namespace LocationAwareness
{
    public partial class GUI : Form
    {
        private Settings settings;
        private Timer timer;
        private PoiPanel poiPanel;
        private int timerInterval;

        public GUI()
        {
            InitializeComponent();

            //Timer
            timer = new Timer();
            timer.Tick += new EventHandler(this.OnTimer);
            //For first update of the poi list
            timer.Interval = 1000000000;    //Setting the timer interval to infinity for not calling the updatePanel method
            menuItemDefault.Click -= menuItemDefault_Click;

            poiPanel = new PoiPanel(this);
            poiPanel.Dock = DockStyle.Fill;
            poiPanel.PanelUpdated += new DelegatePanelUpdated(poiPanel_PanelUpdated);
            this.Controls.Add(poiPanel); 
        }

        /// <summary>
        /// Event method - Loads settings, gps driver and starts cyclinc refresh of webbrowser
        /// </summary>
        private void GUI_Load(object sender, EventArgs e)
        {
            //read out settings and add buttons
            settings = SerializeSettings.loadSettings();
            if (settings == null)
            {
                settings = new Settings();
                settings.ShowButtons = true;
                //settings.ServiceUrl = "http://192.168.0.102/locawa";
                //settings.ServiceUrl = "http://10.1.237.83/locawa";
                //settings.ServiceUrl = "http://192.168.2.100/locawa";
                settings.ServiceUrl = "http://wwwlehre.dhbw-stuttgart.de/~it07029/locawa_xml";

                settings.DefaultFile = "index.php?lat={0}&long={1}";
                settings.DetailFile = "detail.php?lat={0}&long={1}&oid={2}";
                settings.UpdateDetailFile = "updatedetail.php?lat={0}&long={1}&oid={2}";

                settings.Update = 10;
                settings.DistanceToPoi = "1000";
            }
      
            poiPanel.ShowButtons = settings.ShowButtons;
            poiPanel.ServiceUrl = settings.ServiceUrl;
            poiPanel.DefaultFile = settings.DefaultFile;
            poiPanel.DetailFile = settings.DetailFile;
            poiPanel.UpdateDetailFile = settings.UpdateDetailFile;
            poiPanel.SetFilter(settings.DefaultFilter);
            poiPanel.SetMaxDistance(settings.DistanceToPoi);

            if (settings.Update != 0)
            {
                timerInterval = (int)settings.Update * 1000;
                timer.Enabled = true;
            }
        }

        /// <summary>
        /// Event method - Opens SettingsGUI.cs and saves changes Settings
        /// </summary>
        private void menuItemSettings_Click(object sender, EventArgs e)
        {
            //pause cyclic refresh
            pauseTimer();

            SettingsGUI settingsGui = new SettingsGUI(settings);
            if (settingsGui.ShowDialog() == DialogResult.OK)
            {
                settings = settingsGui.Settings;

                poiPanel.ShowButtons = settings.ShowButtons;

                if (settings.Update != 0)
                {
                    timerInterval = (int)settings.Update * 1000;
                    timer.Interval = timerInterval;
                    timer.Enabled = true;
                }
                else if (settings.Update ==0 && timer.Enabled)
                    timer.Enabled = false;

                poiPanel.ServiceUrl = settings.ServiceUrl;
                poiPanel.DefaultFile = settings.DefaultFile;
                poiPanel.DetailFile = settings.DetailFile;
                poiPanel.UpdateDetailFile = settings.UpdateDetailFile;
                poiPanel.SetFilter(settings.DefaultFilter);
                poiPanel.SetMaxDistance(settings.DistanceToPoi);
            }

            //refresh panel
            poiPanel.UpdatePanel();
        }

        /// <summary>
        /// Event method - Closes application.
        /// </summary>
        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Event method - Method occurs before closing. Saves settings and
        /// shutdown gps device
        /// </summary>
        private void GUI_Closing(object sender, CancelEventArgs e)
        {
            if(settings != null)
                SerializeSettings.saveSettings(settings);
        }

        /// <summary>
        /// Event method - Shows default view and refreshs webbrowser.
        /// </summary>
        private void menuItemDefault_Click(object sender, EventArgs e)
        {
            pauseTimer();
            poiPanel.ShowDefaultView();
        }

        //Wg. Performance Tests beim Entfernen auch using System.IO; rausnehmen!!!!
        //DateTime StartZeit;
        //DateTime EndZeit;

        /// <summary>
        /// Event method - Cyclic method refreshs webbrowser.
        /// </summary>
        private void OnTimer(object sender, EventArgs args)
        {
            pauseTimer();    
            //Wg. Performance Tests
            //StartZeit = DateTime.Now;
            poiPanel.UpdatePanel();
        }

        private void poiPanel_PanelUpdated()
        {
            //Wg. Performance Tests
            //EndZeit = DateTime.Now;
            //TimeSpan GemessendeZeit = EndZeit - StartZeit;

            //string s = GemessendeZeit.TotalMilliseconds.ToString() + " | ";
            //FileStream fs = File.Open("TestData.txt", FileMode.Append);
            //byte[] info = new UTF8Encoding(true).GetBytes(s);
            //fs.Write(info, 0, info.Length);
            //fs.Close();

            menuItemDefault.Click += menuItemDefault_Click;
            timer.Interval = timerInterval;     //Setting the timer intervall back
        }

        public void pauseTimer()
        {
            timer.Interval = 1000000000; //Setting the timer interval to infinity for not 
                                         //calling the updatePanel method again 
            menuItemDefault.Click -= menuItemDefault_Click;
        }
    }
}